package com.guigu.www.utils;

import com.guigu.www.pojo.BespeakPoject;
import com.guigu.www.pojo.BespeakSetMeal;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class PDFCreate {

    public static void createPDF(String fileName, BespeakSetMeal setMeal) throws DocumentException, IOException {
        //创建文件
        Document document = new Document();
        document.setPageSize(PageSize.A4);//设置页面大小
        // 获取本机的桌面路径
        File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        String desktopPath = desktopDir.getAbsolutePath();
        //建立一个书写器
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream(desktopPath+"\\"+fileName+".pdf"));
        //打开文件
        document.open();

        BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//设置中文字体
        Font headFont = new Font(bfChinese, 13, Font.NORMAL);//设置字体大小
        Font headFont1 = new Font(bfChinese, 10, Font.NORMAL);
        Font headFont2 = new Font(bfChinese, 10, Font.NORMAL);
        headFont2.setColor(BaseColor.RED);
        Font headFont3 = new Font(bfChinese, 10, Font.NORMAL);
        headFont3.setColor(BaseColor.GREEN);
        Font headFont4 = new Font(bfChinese, 16, Font.NORMAL);
        headFont4.setColor(BaseColor.MAGENTA);

        document.add(new Paragraph("一、个人信息及套餐信息",headFont4));

        //添加有序列表
        com.itextpdf.text.List orderedList = new com.itextpdf.text.List(com.itextpdf.text.List.ORDERED);
        orderedList.add(new ListItem("姓名：",headFont));
        orderedList.add(new ListItem("年龄：",headFont));
        orderedList.add(new ListItem("手机号：",headFont));
        orderedList.add(new ListItem("套餐名称："+setMeal.getBesSmName(),headFont));
        orderedList.add(new ListItem("套餐价格："+setMeal.getBesSmPrice(),headFont));
        orderedList.add(new ListItem("适用年龄："+setMeal.getBesSmAge(),headFont));
        orderedList.add(new ListItem("套餐描述："+setMeal.getBesSmDesc(),headFont));
        document.add(orderedList);

        document.add(new Paragraph("二、体检结果",headFont4));
        com.itextpdf.text.List orderedList1 = new com.itextpdf.text.List(com.itextpdf.text.List.ORDERED);
        orderedList1.add(new ListItem("体检结果说明：",headFont));
        document.add(orderedList1);

        // 3列的表.
        PdfPTable table = new PdfPTable(3);

        table.setWidthPercentage(100); // 宽度100%填充
        table.setSpacingBefore(10f); // 前间距
        table.setSpacingAfter(10f); // 后间距
        table.getDefaultCell().setMinimumHeight(23);


        List<PdfPRow> listRow = table.getRows();
        //设置列宽
        float[] columnWidths = { 3f, 2f, 1f };
        table.setWidths(columnWidths);

        //行1
        PdfPCell[] cells1= new PdfPCell[3];
        PdfPRow row1 = new PdfPRow(cells1);

        document.add(new Paragraph("三、套餐体检项目详情",headFont4));

        //单元格
        cells1[0] = new PdfPCell(new Paragraph("项目名称",headFont));//单元格内容
        cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
        cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中

        cells1[1] = new PdfPCell(new Paragraph("体检结果",headFont));
        cells1[1].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
        cells1[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中

        cells1[2] = new PdfPCell(new Paragraph("正常参考值",headFont));
        cells1[2].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
        cells1[2].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中

        //把第一行添加到集合
        listRow.add(row1);
        for (BespeakPoject project : setMeal.getList()) {
            //行2
            PdfPCell[] cells2= new PdfPCell[3];
            PdfPRow row2 = new PdfPRow(cells2);
            cells2[0] = new PdfPCell(new Paragraph(project.getBesProName(),headFont1));
            cells2[1] = new PdfPCell(new Paragraph("——",headFont2));
            cells2[2] = new PdfPCell(new Paragraph(project.getQuota().getQuotaConsult()+project.getQuota().getQuotaUnit(),headFont3));
            cells2[2].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
            cells2[2].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
            listRow.add(row2);
        }

        //把表格添加到文件中
        document.add(table);

        document.add(new Paragraph("四、注意事项及说明",headFont4));
        com.itextpdf.text.List orderedList2 = new com.itextpdf.text.List(com.itextpdf.text.List.ORDERED);
        orderedList2.add(new ListItem("注意事项：",headFont));
        orderedList2.add(new ListItem("说明：",headFont));
        document.add(orderedList2);

        //设置属性
        //标题
        document.addTitle("模板文件");
        //作者
        document.addAuthor("DengHao");
        //主题
        document.addSubject("套餐检查项");
        //关键字
        document.addKeywords("Keywords");
        //创建时间
        document.addCreationDate();

        //关闭文档
        document.close();
        //关闭书写器
        writer.close();
        // System.out.println("成功！！！");
    }
}
